package com.xn.myyxcampus;

import java.io.IOException;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.xn.myyxcampus.bean.NewsDetail;
import com.xn.myyxcampus.bean.NewsDetailList;
import com.xn.myyxcampus.http.URLHelper;
import com.xn.myyxcampus.http.URLs;
import com.xn.myyxcampus.utils.ActivityUtils;
import com.xn.myyxcampus.utils.AnimUtils;
import com.xn.myyxcampus.utils.JacksonUtils;
import com.xn.myyxcampus.utils.TimeUtil;
import com.xn.myyxcampus.utils.ToastUtils;

public class NewsDetailActivity extends BaseActivity implements OnClickListener {

	private ImageView iv_loadImg;
	private ImageView iv_goBack;
	private AnimationDrawable animationDrawable;
	private LinearLayout detail_newsLayout;
	private TextView tv_newsTitle;
	private TextView tv_newsTime;
	private TextView tv_newsClick;

	private ImageView iv_newsDetailImg;
	private TextView tv_newsDetailContent;

	private Intent intent;

	private String bitmapCache = MyApplication.getContext()
			.getExternalCacheDir() + "/tracy/News/";
	private BitmapUtils bitmapUtil = new BitmapUtils(
			MyApplication.getContext(), bitmapCache);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail);
		ActivityUtils.addToActivityStack(NewsDetailActivity.this);
		iv_loadImg = (ImageView) findViewById(R.id.iv_newsDetailLoad);
		iv_loadImg.setBackgroundResource(R.drawable.loading_anim);
		animationDrawable = (AnimationDrawable) iv_loadImg.getBackground();
		animationDrawable.start();

		httpUtil = new HttpUtils();
		initView();
		initData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_goBack = (ImageView) findViewById(R.id.iv_goBack);
		detail_newsLayout = (LinearLayout) findViewById(R.id.detail_newsLayout);
		tv_newsTitle = (TextView) findViewById(R.id.tv_newsTitle);
		tv_newsTime = (TextView) findViewById(R.id.tv_newsTime);
		tv_newsClick = (TextView) findViewById(R.id.tv_newsClick);

		iv_newsDetailImg = (ImageView) findViewById(R.id.iv_newsDetailImg);
		tv_newsDetailContent = (TextView) findViewById(R.id.tv_newsDetailContent);

		iv_goBack.setOnClickListener(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		intent = getIntent();
		String newsID = intent.getStringExtra("newsID");
		NameValuePair id_pair = new BasicNameValuePair("id", newsID);
		LogUtils.i(URLHelper.getURL(URLs.NEWS_DETAIL, id_pair));
		httpUtil.send(HttpRequest.HttpMethod.GET,
				URLHelper.getURL(URLs.NEWS_DETAIL, id_pair),
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						ToastUtils.showToast("网络异常~~~淡定~~~");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						// loadData(arg0.result);
						LogUtils.i(arg0.statusCode + "");
						LogUtils.i("newsDetail" + arg0.result);
						loadData(arg0.result);
					}
				});

	}

	private void loadData(String response) {
		try {
			NewsDetail newsDetail = JacksonUtils.jsonToBean(response,
					NewsDetail.class);
			NewsDetailList list = newsDetail.getList();
			if (1 == newsDetail.getResponse()) {
				animationDrawable.stop();
				detail_newsLayout.setVisibility(View.VISIBLE);
				String newspic = list.getNewspic();
				
				tv_newsTitle.setText(list.getTitle());
				tv_newsTime.setText(TimeUtil.getCurrrentTime(Long
						.parseLong(list.getTime())));
				tv_newsClick.setText(list.getClick());
				tv_newsDetailContent.setText(list.getContent());
				
				bitmapUtil.configDefaultLoadFailedImage(R.drawable.empty_photo);
				bitmapUtil.configDefaultLoadingImage(R.drawable.empty_photo);
				bitmapUtil.display(iv_newsDetailImg,
						URLs.NEWS_PHOTO + newspic.replace('+', '/'));
				iv_loadImg.setVisibility(View.GONE);
			} else {
				ToastUtils.showToast("没有对应的内容，淡定~~~");
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_goBack:
			NewsDetailActivity.this.finish();
			AnimUtils.scaleOut(NewsDetailActivity.this);
		}
	}
}
